package com.doupi.partner.op.controller.market;

import com.doupi.partner.dal.model.SystemConfig;
import com.doupi.partner.op.constant.OpConstants;
import com.doupi.partner.op.util.OpUtils;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.service.dto.AdministratorDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 推广
 *
 * @author kang xiaojun
 * @version 1.0.0 2017-04-25
 **/
@Controller
@RequestMapping("market")
public class MarketController {
	private static final String TEMPLATE_FOLDER = "market/";

	// 合伙人推广链接值
	private final String PARTNER_PROMOTION_URL_VALUE;

	// 商家推广链接值
	private final String BUSINESS_PROMOTION_URL_VALUE;

	@Autowired
	public MarketController(SystemConfigService systemConfigService) {
		SystemConfig systemConfig = systemConfigService.selectByPrimaryKey(OpConstants.PARTNER_PROMOTION_URL_KEY);
		PARTNER_PROMOTION_URL_VALUE = systemConfig.getValue();
		systemConfig = systemConfigService.selectByPrimaryKey(OpConstants.BUSINESS_PROMOTION_URL_KEY);
		BUSINESS_PROMOTION_URL_VALUE = systemConfig.getValue();
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(ModelMap modelMap) {
		AdministratorDto currentLoginAdministrator = OpUtils.getCurrentLoginAdministrator();
		String marketingNumber = currentLoginAdministrator.getPartnerRelation().getMarketingNumber();

		modelMap.put("partnerUrl", StringUtils.replaceAll(PARTNER_PROMOTION_URL_VALUE, "\\{marketingNumber\\}", marketingNumber));
		modelMap.put("businessUrl", StringUtils.replaceAll(BUSINESS_PROMOTION_URL_VALUE, "\\{marketingNumber\\}", marketingNumber));
		return TEMPLATE_FOLDER + "index";
	}
}
